#!/usr/bin/python2.5 

import sys
sys.path.append("../")

import time
from runtime import *
from game import *
from pygame.locals import *


"""
RE-WRITTEN:
13 July 2008 to work with the new real time physics system
"""




def create_pickled_game():
    """
    ############################################################################
    START OF GAME CODE
    """

    # Create a new Game
    mytestGame = Game("Platform - GameBaker Demo")

    # Create a Game-Screen / Level
    mytestgamescreen = mytestGame.gamescreen((1000,600),"level")

    completedscreen = mytestGame.gamescreen((800,600),"levelcomplete")
    completedscreen.color=(255,0,0)

    # Load this screen first
    mytestGame.startscreen = "level"

    # Create a GameObject to show in the game - will later need a sprite attatched
    floor = mytestGame.gameobject("floor")
    plat1 = mytestGame.gameobject("plat1")
    plat2 = mytestGame.gameobject("plat2")
    plat3 = mytestGame.gameobject("plat3")
    plat4 = mytestGame.gameobject("plat4")
    plat5 = mytestGame.gameobject("plat5")
    plat6 = mytestGame.gameobject("plat6")
    plat7 = mytestGame.gameobject("plat7")
    flag = mytestGame.gameobject("flag")
    player = mytestGame.gameobject("player")

    # Say that we want this object to appear immediately in our game-screen
    mytestgamescreen.startobjects.append(floor)
    mytestgamescreen.startobjects.append(player)
    mytestgamescreen.startobjects.append(plat1)
    mytestgamescreen.startobjects.append(plat2)
    mytestgamescreen.startobjects.append(plat3)
    mytestgamescreen.startobjects.append(plat4)
    mytestgamescreen.startobjects.append(plat5)
    mytestgamescreen.startobjects.append(plat6)
    mytestgamescreen.startobjects.append(plat7)
    mytestgamescreen.startobjects.append(flag)


    # Create a score object
    score = mytestGame.gameobject("Score")
    mytestgamescreen.startobjects.append(score)



    # Set up a workstate for our objects


    flag.baseworkstate="flag"
    wsplat = mytestGame.workstate("flag")
    wsplat.actions[EVENT_INIT] = """
self.x=550
self.y=100
self.vx=0
self.vy=0
self.resize(32,37)
"""


    wsplat.actions[EVENT_ITERATION] = """
if gamescreen.check_collisions("player","flag"):
    game.score +=1
    gamescreen.get_object("player").workstate = "wsplayercelebrate"
"""
    flag.sprite = "flag_sprite"

    plat1.baseworkstate="wsplat"
    wsplat = mytestGame.workstate("wsplat")
    wsplat.actions[EVENT_INIT] = """
self.x=270
self.y=450
self.vx=0
self.vy=0
self.resize(150,8)
"""


    plat2.baseworkstate="wsplat2"
    wsplat = mytestGame.workstate("wsplat2")
    wsplat.actions[EVENT_INIT] = """
self.x=0
self.y=510
self.vx=0
self.vy=0
self.resize(180,8)
"""

    plat3.baseworkstate="wsplat3"
    wsplat = mytestGame.workstate("wsplat3")
    wsplat.actions[EVENT_INIT] = """
self.x=60
self.y=380
self.vx=0
self.vy=0
self.resize(80,8)
"""

    plat4.baseworkstate="wsplat4"
    wsplat = mytestGame.workstate("wsplat4")
    wsplat.actions[EVENT_INIT] = """
self.x=0
self.y=320
self.vx=0
self.vy=0
self.resize(60,8)
"""

    plat5.baseworkstate="wsplat5"
    wsplat = mytestGame.workstate("wsplat5")
    wsplat.actions[EVENT_INIT] = """
self.x=0
self.y=250
self.vx=0
self.vy=0
self.resize(60,8)
"""

    plat6.baseworkstate="wsplat6"
    wsplat = mytestGame.workstate("wsplat6")
    wsplat.actions[EVENT_INIT] = """
self.x=0
self.y=210
self.vx=50
self.vy=0
self.resize(60,8)
"""


    wsplat.actions[EVENT_ITERATION] = """
self.move_velocity()
if self.x < 0 or self.x > 400:
    self.vx = - self.vx
"""

    plat7.baseworkstate="wsplat7"
    wsplat = mytestGame.workstate("wsplat7")
    wsplat.actions[EVENT_INIT] = """
self.x=400
self.y=140
self.vx=0
self.vy=0
self.resize(250,8)
"""

    wsplayercomplete = mytestGame.workstate("wsplayercelebrate")
    wsplayercomplete.actions[EVENT_INIT] = """
self.sprite = "soldier_applaud"
self.vx = 0
self.vy = 0
self.vars["count"] = 0
"""

    wsplayercomplete.actions[EVENT_ITERATION] = """
self.vars["count"] +=1
if self.vars["count"] > 40:
    game.change_game_screen("levelcomplete")    
"""
    player.baseworkstate="wsplayerstart"
    wsplayerstart = mytestGame.workstate("wsplayerstart")
    wsplayerstart.actions[EVENT_INIT] = """
gamescreen.color=[15,0,15]

self.x=700
self.y=500
self.vx=0
self.vy=0
self.ax=0.
self.ay=6.0
self.resize(38,48)
"""

    wsplayerstart.actions[EVENT_ITERATION] = """
gamescreen.move_gamescreen([self.x,self.y],gradual=True,acceleration=1)

self.onsurface=False
for surface in ["floor","plat1","plat2","plat3","plat4","plat5","plat6","plat7"]:
    if gamescreen.check_collisions("player",surface):
        if self.y + self.height - 8 < gamescreen.get_object(surface).y:
            self.onsurface=True


if self.x < 0:
    if self.vx < 0:
        self.vx = 0
        self.x = 0
if self.x + self.width > gamescreen.width:
    if self.vx > 0:
        self.vx = 0
        self.x = gamescreen.width - self.width


self.vy += 0.2 + self.ay


if self.vy > 130:
    self.vy = 130

if self.onsurface:
    self.vx += self.ax
    # If Falling Down
    if self.vy > 0:
        self.vy = 0

    # apply drag
    self.vx = self.vx * 0.9

if abs(self.vx) > 0.5:
    if self.vx > 0:
        self.sprite = "soldier_runright"
    else:
        self.sprite = "soldier_runleft"
else:
    self.sprite = "soldier_idle"


self.move_velocity()
"""

    wsplayerstart.actions[EVENT_KEYDOWN*K_RIGHT] = """
if self.vx < 100:
    self.ax = 12.5
else:
    self.ax = 0.
"""

    wsplayerstart.actions[EVENT_KEYUP*K_RIGHT] = """
self.ax = 0.0
"""

    wsplayerstart.actions[EVENT_KEYUP*K_LEFT] = """
self.ax = 0.0
"""

    wsplayerstart.actions[EVENT_KEYDOWN*K_LEFT] = """

if self.vx > -100:
    self.ax = -12.5
else:
    self.ax = 0.
"""

    wsplayerstart.actions[EVENT_KEYDOWN*K_UP] = """
if abs(self.vy) < 7:
    self.vy =- 150
"""


    ws1 = mytestGame.workstate("wsfloor")

    ws1.actions[EVENT_INIT] = """
self.x=0
self.y=575
self.resize(1200,25)
"""



    # Tell the GameObject to start with this workstate
    floor.baseworkstate = "wsfloor"

    wsscore = mytestGame.workstate("WSScore")

    wsscore.actions[EVENT_INIT] = """
self.x = 0 
self.y = 0
self.color = (255,255,255)
self.resize((60,20))
self.clear()
"""
    wsscore.actions[EVENT_ITERATION] = """

self.clear()
self.draw_text(game.score,20,(255,0,0))
"""
    score.baseworkstate = "WSScore"




    sprite = mytestGame.sprite("soldier_applaud")

    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,0), "bottomright":(39,50)})
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,50), "bottomright":(39,100)})
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,100), "bottomright":(39,150)})
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,150), "bottomright":(39,200)})
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,200), "bottomright":(39,250)})
    sprite.imagefiles.append({"filename":"images/soldier_applaud.bmp", "topleft":(1,250), "bottomright":(39,300)})
    sprite.framerate = 2
    sprite.alphakey = (255,0,0)

    # Now Create the Sprites
    sprite = mytestGame.sprite("soldier_runleft")

    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,0), "bottomright":(39,48)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,48), "bottomright":(39,96)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,96), "bottomright":(39,144)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,144), "bottomright":(39,192)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,192), "bottomright":(39,240)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,240), "bottomright":(39,288)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,288), "bottomright":(39,336)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,336), "bottomright":(39,384)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,384), "bottomright":(39,432)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,432), "bottomright":(39,480)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,480), "bottomright":(39,528)})
    sprite.imagefiles.append({"filename":"images/soldier_runleft.bmp", "topleft":(1,528), "bottomright":(39,576)})


    # update the sprite frame every two game frames
    sprite.framerate = 2
    sprite.alphakey = (255,0,0)

    #tell the main gameobject to use this
    player.sprite = "soldier_runright"

    sprite = mytestGame.sprite("soldier_runright")
    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,0), "bottomright":(39,48)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,48), "bottomright":(39,96)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,96), "bottomright":(39,144)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,144), "bottomright":(39,192)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,192), "bottomright":(39,240)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,240), "bottomright":(39,288)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,288), "bottomright":(39,336)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,336), "bottomright":(39,384)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,384), "bottomright":(39,432)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,432), "bottomright":(39,480)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,480), "bottomright":(39,528)})
    sprite.imagefiles.append({"filename":"images/soldier_runright.bmp", "topleft":(1,528), "bottomright":(39,576)})

    # update the sprite frame every two game frames
    sprite.framerate = 2
    sprite.alphakey = (255,0,0)




    sprite = mytestGame.sprite("soldier_idle")
    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,2), "bottomright":(39,50)})
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,52), "bottomright":(39,100)})
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,102), "bottomright":(39,150)})
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,152), "bottomright":(39,200)})
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,102), "bottomright":(39,250)})
    sprite.imagefiles.append({"filename":"images/soldier_idle.bmp", "topleft":(1,252), "bottomright":(39,300)})

    import gbfileio
    gbfileio.savegame("soldier_idle.gbs",sprite,typ="sprite")

    # update the sprite frame every two game frames
    sprite.framerate = 2
    sprite.alphakey = (255,0,0)

    sprite = mytestGame.sprite("flag_sprite")
    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,4), "bottomright":(37,41)})
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,52), "bottomright":(37,89)})
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,100), "bottomright":(37,137)})
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,148), "bottomright":(37,185)})
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,196), "bottomright":(37,233)})
    sprite.imagefiles.append({"filename":"images/flag_sprite.bmp", "topleft":(4,244), "bottomright":(37,281)})



    # update the sprite frame every two game frames
    sprite.framerate = 1
    sprite.alphakey = (255,0,0)



    plat1.sprite = "platform_sprite"
    plat2.sprite = "platform_sprite"
    plat3.sprite = "platform_sprite"
    plat4.sprite = "platform_sprite"
    plat5.sprite = "platform_sprite"
    plat6.sprite = "platform_sprite"
    plat7.sprite = "platform_sprite"


    sprite = mytestGame.sprite("ground_sprite")
    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/groundsprite.bmp", "topleft":(0,0), "bottomright":(1300,49)})
    # update the sprite frame every two game frames
    sprite.framerate = 200
    sprite.alphakey = (255,0,0)

    sprite = mytestGame.sprite("platform_sprite")
    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"images/groundsprite.bmp", "topleft":(100,5), "bottomright":(400,20)})
    # update the sprite frame every two game frames
    sprite.framerate = 200
    sprite.alphakey = (255,0,0)

    floor.sprite = "ground_sprite"

    """
    *********************************************************************************
    END OF GAME CODE
    """

    # Pickle it up - could replace with output to XML etc.
    import cPickle
    FILE = open("testgame.gb", 'w')
    #cPickle.dump(mytestGame, FILE)
    try:
        import gbfileio
        FILE.write(gbfileio.makeyaml(mytestGame))
    except:
        cPickle.dump(mytestGame, FILE)
    FILE.close()

    import gbfileio
    gbfileio.savegame("gameouttestyaml.gb",mytestGame,method="pickle")

    game = gbfileio.loadgame("gameouttestyaml.gb")

    run_game(game)
    """
    import gbcompile
    gbcompile.compile_to_python(mytestGame,"out.py")
    """



if __name__ == "__main__":
    create_pickled_game()
    """
    try:
        play_yaml_game()
    except:
        play_pickled_game()
"""
